﻿using System;
using System.Collections.Generic;
using System.Text;
using Magnet.Engine;
using Magnet.Engine.Entities;
using Magnet.Engine.Physics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Magnet.Engine.Entities
{
    public class PlayerManager
    {
        public enum GameState
        {
            LevelTransition, BulletTime, Normal, Respawn, Dying
        }

        public GameState gameState = GameState.Normal;

        public static float bulletTimer = 0f;
        public const float bulletLimit = 100f;

        private const float RESPAWN_DELAY = 3f;
        private const float DYING_DELAY = 1.5f;
        private const float BULLET_TIME_DELAY = 2f;

        private Player player;

        float timer;
        float timerDelay;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        public PlayerManager(Player player)
        {
            this.player = player;
            player.StartAnimation(5, Player.PlayerState.Normal, true);
            bulletTimer = 0f;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elapsedSeconds"></param>
        /// 
        public virtual void Update(float elapsedSeconds)
        {
            Collisions();
            checkState(elapsedSeconds);
            checkInput();
            checkMouseSelection();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Collisions()
        {
            player.StartAnimation(5, Player.PlayerState.Normal, true);
            List<Arbiter> Arbiter = World.GetRelevantCollisions(player);

            foreach (Arbiter a in Arbiter)
            {
                Entity e = a.GetOtherEntity(player);
                if (e is Magnet)
                {
                    gameState = GameState.Dying;
                    player.StartAnimation(5, Player.PlayerState.Dying, true);

                }
                else if (e is Goal)
                {
                    gameState = GameState.LevelTransition;
                }
                else if (e is Debris)
                {
                    gameState = GameState.Dying;
                    player.StartAnimation(5, Player.PlayerState.Dying, true);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void checkState(float elapsedSeconds)
        {
            if (gameState == GameState.BulletTime)
            {
                if (bulletTimer == 0)
                {
                    gameState = GameState.Normal;
                    timer = 0f;
                }

                bulletTimer -= elapsedSeconds * 20f;

                if (bulletTimer < 0f)
                    bulletTimer = 0f;
            }
            else if (gameState == GameState.Dying)
            {
                if (timer >= timerDelay)
                {
                    gameState = GameState.Respawn;
                    timerDelay = RESPAWN_DELAY;
                    player.active = false;
                }

                timer += elapsedSeconds;
            }
            else if (gameState == GameState.Respawn)
            {
                if (timer >= timerDelay)
                {
                    gameState = GameState.Normal;
                    player.active = true;
                    timer = 0f;
                }

                timer += elapsedSeconds;
            }
            else if (gameState == GameState.LevelTransition)
            {
                if (timer >= timerDelay)
                {
                    GameplayScreen.changeLevel = true;
                    gameState = GameState.Respawn;
                    timerDelay = RESPAWN_DELAY;
                    player.active = false;
                }

                timer += elapsedSeconds;
            }
            else
            {
                if (bulletTimer < bulletLimit)
                    bulletTimer += elapsedSeconds * 10;
                else if (bulletTimer > bulletLimit)
                    bulletTimer = bulletLimit;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void checkInput()
        {
            //Move
            /*if (InputManager.IsKeyPressed(InputManager.W))
            {
                player.Position = new Vector2(player.Position.X, player.Position.Y - 2.0f);
            }
            if (InputManager.IsKeyPressed(InputManager.S))
            {
                player.Position = new Vector2(player.Position.X, player.Position.Y + 2.0f);
            }
            if (InputManager.IsKeyPressed(InputManager.A))
            {
                player.Position = new Vector2(player.Position.X - 2.0f, player.Position.Y);
            }
            if (InputManager.IsKeyPressed(InputManager.D))
            {
                player.Position = new Vector2(player.Position.X + 2.0f, player.Position.Y);
            }*/

            //Rotate
            if (InputManager.IsKeyPressed(InputManager.A))
            {
                player.StartAnimation(1, Player.PlayerState.RotateLeft, true);
                player.Rotation -= 0.1f;
            }
            else if (InputManager.IsKeyPressed(InputManager.D))
            {
                player.StartAnimation(1, Player.PlayerState.RotateRight, true);
                player.Rotation += 0.1f;
            }

            if (InputManager.IsKeyPressed(Keys.Space))
            {
                gameState = GameState.BulletTime;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void checkMouseSelection()
        {
            Magnet selected = null;

            if (InputManager.isLeftMouseButtonTriggered())
            {
                int length = World.GetEntityCount();

                Vector2 mousePosition = InputManager.MousePosition;

                Magnet m;

                for (int i = 0; i < length; i++)
                {
                    m = World.GetEntityAt(i) as Magnet;

                    if (m != null)
                    {
                        if ((m as Player) != player)
                        {
                            if (m.Intersects(mousePosition))
                            {
                                selected = m;
                                break;
                            }
                        }
                    }
                }
            }
            
            if (selected != null)
            {
                selected.attraction = -selected.attraction;
            }
        }

        public Player GetPlayer()
        {
            return player;
        }
    }
}
